<template>
  <el-dropdown trigger="click" @command="handleSetSize">
    <div style="display: flex; align-items: center; height: 100%">
      <svg-icon class-name="size-icon" icon-class="size" />
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item
          v-for="item of sizeOptions"
          :key="item.value"
          :disabled="size === item.value"
          :command="item.value"
        >
          {{ item.label }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import router from '@/router'

// 定义尺寸选项
const sizeOptions = ref([
  { label: 'Default', value: 'default' },
  { label: 'Medium', value: 'medium' },
  { label: 'Small', value: 'small' },
  { label: 'Mini', value: 'mini' },
])

// 获取store实例
const store = useStore()

// 计算属性获取当前尺寸
const size = computed(() => {
  return store.state.app.size
})

// 设置尺寸处理函数
const handleSetSize = (size: string) => {
  // 设置Element Plus的尺寸
  // todo: 临时解决element plus的size属性不生效的问题
  // window.ELEMENT = { size }
  store.dispatch('app/setSize', size)
  refreshView()
  ElMessage({
    message: 'Switch Size Success',
    type: 'success',
  })
}

// 刷新视图
const refreshView = () => {
  // 为了使缓存的页面重新渲染
  store.dispatch('tagsView/delAllCachedViews', store.state.tagsView.visitedViews)

  const { fullPath } = router.currentRoute.value

  // 使用nextTick确保路由操作在DOM更新后执行
  setTimeout(() => {
    window.location.hash = `#${fullPath}`
    window.location.reload()
  }, 100)
}
</script>
